package com.sugon.controller.device;

import cn.hutool.core.util.NumberUtil;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.DevAmRulesEntity;
import com.sugon.entity.DevAmRulesOptionEntity;
import com.sugon.entity.DevMainBaseAmEntity;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.modules.device.model.vo.AddMaintenanceRuleItemVO;
import com.sugon.modules.device.service.IDeviceAmRulesManager;
import com.sugon.service.DevAmRulesOptionService;
import com.sugon.service.DevAmRulesService;
import com.sugon.service.DevMainBaseAmService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 设备自主维护规程选项关联表Controller
 *
 * @author YuXD
 * @date 2023-01-23
 */
@Controller
@RequestMapping("devAmMaintenanceOption")
public class DevAmRulesOptionController {
    @Resource
    private DevAmRulesOptionService devAmRulesOptionService;
    @Resource
    private IDeviceAmRulesManager deviceAmRulesManager;
    @Resource
    private DevMainBaseAmService devAmItemService;
    @Resource
    private DevAmRulesService devAmRulesService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<DevAmRulesOptionEntity> devPlanOptionList = devAmRulesOptionService.queryList(query);
        int total = devAmRulesOptionService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(devPlanOptionList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("devplanoption:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevAmRulesOptionEntity devPlanOption = devAmRulesOptionService.queryObject(id);

        return R.ok().put("devPlanOption", devPlanOption);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestParam String planId, @RequestParam String mainBaseId) {
        // 1、查重
        int rows = devAmRulesOptionService.isHave(planId, mainBaseId);
        if (rows > 0) {
            return R.error("当前维护项已关联该规程，请更换!");
        }
        DevAmRulesOptionEntity dpo = new DevAmRulesOptionEntity();
        dpo.setPlanId(planId);
        dpo.setMainBaseId(mainBaseId);
        return devAmRulesOptionService.save(dpo) > 0 ? R.ok("添加成功") : R.error("添加失败，请稍后重试");
    }

    @NonBusinessParamValid
    @RequestMapping("/batchSave")
    @ResponseBody
    public R batchSave(@Validated @RequestBody AddMaintenanceRuleItemVO addMaintenanceRuleItem, BindingResult bindingResult) {
         /*
            保存维护项时有两种情况：已审核通过的 、 其它
            如果是审核通过的，那么需要克隆生成新的设备维护规程
            如果是其它，则可以直接保存
         */
        DevAmRulesEntity devAmRules = devAmRulesService.queryObject(addMaintenanceRuleItem.getPlanId());
        Assert.notNull(devAmRules, "维护规程[" + addMaintenanceRuleItem.getPlanId() + "]不存在，请联系管理员！");
        if (NumberUtil.equals(ProcessProgressConst.PASSED, devAmRules.getProgress())) {
            // 克隆时需要排除的维护项ID
            devAmRules = deviceAmRulesManager.cloneDevPmRule(addMaintenanceRuleItem.getPlanId(), null, addMaintenanceRuleItem.getMainBaseIdList());
        } else {
            List<String> mainBaseIdList = addMaintenanceRuleItem.getMainBaseIdList();
            for (String mainBaseId : mainBaseIdList) {
                DevMainBaseAmEntity devAmItem = devAmItemService.queryObject(mainBaseId);
                if (devAmItem == null) {
                    return R.error("非法设备维护项ID！");
                }
                int rows = devAmRulesOptionService.isHave(devAmRules.getId(), mainBaseId);
                if (rows > 0) {
                    return R.error("维护项[{}]已关联该规程，请更换!", devAmItem.getName());
                }
                // 保存设备维护规程 项 信息
                devAmRulesOptionService.save(new DevAmRulesOptionEntity(devAmRules.getId(), mainBaseId));
            }
        }
        return R.ok("添加成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("devplanoption:update")
    @ResponseBody
    public R update(@RequestBody DevAmRulesOptionEntity devPlanOption) {
        devAmRulesOptionService.update(devPlanOption);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
//    @RequiresPermissions("devplanoption:delete")
    @ResponseBody
    public R delete(@PathVariable("id") String id) {
        DevAmRulesEntity devPmRule = deviceAmRulesManager.deleteMaintenanceRuleOption(id);
        return R.ok().put("id", devPmRule.getId());
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevAmRulesOptionEntity> list = devAmRulesOptionService.queryList(params);

        return R.ok().put("list", list);
    }
}
